/**
 *
 */
package com.evil.DAO;

import com.evil.Bean.UserBean;
import com.evil.DB_Utils.DB_Utils;
import com.evil.DB_Utils.QueryCallBack;
import com.evil.DB_Utils.UpdateSqlUrl;
import com.evil.Data.Data;

import java.sql.Blob;
import java.text.SimpleDateFormat;
import java.util.Date;

/******************************************
 * 工程名：EdaSupport<br>
 * 包名：com.evil.User<br>
 * 时间：2016年7月24日 上午10:38:09<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：User 数据库操作<br>
 ******************************************/
public class UserDAO
{
    private String re;

    /**
     * 插入数据 注册 包含 用户手机（主键），密码，imei
     *
     * @param bean
     * @return 错误信息
     */
    public String insert_register(UserBean bean)
    {
        String url = "INSERT INTO `edasupport`.`user` (`phone`, `imei`, `registration_time`, `lastlogin_time`)"
                + " VALUES (?, ?, ?, ?);";

        re = DB_Utils
                .insert(url)
                .add(bean.getPhone())
                .add(bean.getImei())
                .add(new Date())
                .add(new Date()).build().close();
        return re;
    }

    /**
     * 查找数据 判断手机号是否在邯郸
     *
     * @return
     */
    public String query_phoneInHanDan(String userPhone)
    {
        String url = "SELECT\n" + "handanphone.handanphone,\n"
                + "handanphone.num\n" + "FROM\n" + "handanphone\n" + "WHERE\n"
                + "handanphone.handanphone = ?";
        String phoneHead = userPhone.substring(0, 7);

        DB_Utils.query(url).add(phoneHead).build().get(next ->
        {
            if (next.next())
            {
                if (next.getInt(2) < 200)
                    re = "IN_HANDAN_SMALL";
                else
                    re = "IN_HANDAN";
            }
            else
                re = "NOT_IN_HANDAN";
        }).close();

        return re;
    }

    /**
     * 查找数据 登陆
     *
     * @return 全部的UserBean
     */
    public Object query_login(UserBean userBean)
    {
        String url = "SELECT\n" +
                "`user`.phone,\n" +
                "`user`.`name`,\n" +
                "`user`.imei,\n" +
                "`user`.img,\n" +
                "`user`.integral,\n" +
                "`user`.blacklist,\n" +
                "`user`.registration_time,\n" +
                "`user`.lastlogin_time,\n" +
                "`user`.times\n" +
                "FROM\n" +
                "`user`\n" +
                "WHERE\n" +
                "`user`.phone = ?";
        UserBean bean = new UserBean();

        DB_Utils.query(url)
                .add(userBean.getPhone())
                .build().get(next ->
        {
            if (next.next())
            {
                bean.setPhone(next.getString(1));
                bean.setName(next.getString(2));
                bean.setImei(next.getString(3));
                bean.setImg((Blob) next.getObject(4));
                bean.setIntegral(next.getInt(5));
                /*如果为0，则为真，表示在黑名单中,如果为1，则为假，不在黑名单中*/
                bean.setBlackList(next.getInt(6) == 1);
                bean.setRegistration_time(next.getDate(7));
                bean.setLastlogin_time(next.getDate(8));
                bean.setTimes(next.getInt(9));
                next.close();
            }
            else
            {
                re = insert_register(userBean);
            }
        }).close();

        if (bean.getPhone() == null)
            return re;
        else
            return bean;
    }

    /**
     * 记录最后一次登陆时间
     */
    public void updateLastLoginTime(String phone)
    {
        String url = "UPDATE `edasupport`.`user` SET `lastlogin_time`=?, `times`=times+1 WHERE (`phone`=?);";

        DB_Utils.update(url)
                .add(new SimpleDateFormat("yyyy-MM-dd HH-mm-ss")
                        .format(new Date())).add(phone).build().close();

    }
}
